package demo.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cl.gob.ips.offline.util.Tarea;

import EDU.oswego.cs.dl.util.concurrent.Takable;


public class ParseaSalida {

	 public List<Tarea> parseaSalidaListaTareas(String mensajeSOAP) {
		 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( );
			Document documento = null;
			InputStream is = null;

			try {
			 is = new ByteArrayInputStream(mensajeSOAP.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				System.out.println(e.getMessage());
			}
			try
			{
			   DocumentBuilder builder = factory.newDocumentBuilder();
			   documento = builder.parse(is);
			}
			catch (Exception spe)
			{
				System.out.println(spe.getMessage());
			}
			
			List<Tarea> listaTareas = new ArrayList<Tarea>();
			Node nodoRaiz = documento.getFirstChild();
			NodeList listaNodosHijos = nodoRaiz.getChildNodes();
			Node hijo;
			Tarea task = new Tarea();
			NodeList tareas = null;
			for (int i=0; i<listaNodosHijos.getLength(); i++){
			   hijo = listaNodosHijos.item(i);
			  if(hijo.getNodeName().equalsIgnoreCase("env:Body")){
				   tareas = hijo.getChildNodes();
				   System.out.println("TAREAS --> "+tareas.getLength());
			  }
			 
			}
			 if(null!=tareas){
				  for (int j = 0; j < tareas.getLength(); j++) {
						Node tarea = tareas.item(j);
						NodeList elementos= tarea.getChildNodes();
						String elem = elementos.item(1).getTextContent();
						String[] el = elem.split("\n");
						
						task.setId(el[1].trim());
						task.setNombre(el[2].trim());
						task.setDescripcion(el[3].trim());
						listaTareas.add(task);
					}
			  }
			return listaTareas;
	 }
	 
	 public String parseaSalidaInicioProceso(String mensajeSOAP) {
		 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( );
			Document documento = null;
			InputStream is = null;

			try {
			 is = new ByteArrayInputStream(mensajeSOAP.getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e) {
				System.out.println(e.getMessage());
			}
			try
			{
			   DocumentBuilder builder = factory.newDocumentBuilder();
			   documento = builder.parse(is);
			}
			catch (Exception spe)
			{
				System.out.println(spe.getMessage());
			}
			
			Node nodoRaiz = documento.getFirstChild();
			NodeList listaNodosHijos = nodoRaiz.getChildNodes();
			Node hijo;
			Tarea task = new Tarea();
			NodeList key = null;
			for (int i=0; i<listaNodosHijos.getLength(); i++){
			   hijo = listaNodosHijos.item(i);
			  if(hijo.getNodeName().equalsIgnoreCase("env:Body")){
				   key = hijo.getChildNodes();
			  }
			 
			}
			String id="";
			 if(null!=key){
				  for (int j = 0; j < key.getLength(); j++) {
						Node keys = key.item(j);
						NodeList elementos= keys.getChildNodes();
						id = elementos.item(1).getTextContent();
					}
			  }
			return id;
	 }
	
}
